package com.huadao.cart_ccs.common.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class CommonRecycleAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {
    protected LayoutInflater layoutInflater;
    protected List<T> dataList;
    protected int layoutId;
//    protected MultiTypeSupport<T> multiTypeSupport;

    private int headerLayoutId;
    private int footerLayoutId;

    private int ITEM_COMMON=1;
    private int ITEM_HEADER=-1;
    private int ITEM_FOOTER=-1;

    public CommonRecycleAdapter(Context context, List<T> dataList, int layoutId) {
        layoutInflater=LayoutInflater.from(context);
        this.dataList = dataList;
        this.layoutId = layoutId;
    }

    public CommonRecycleAdapter(Context context, List<T> dataList, int layoutId,int footerLayoutId) {
        layoutInflater=LayoutInflater.from(context);
        this.dataList = dataList;
        this.layoutId = layoutId;
        this.footerLayoutId=footerLayoutId;
        ITEM_FOOTER=footerLayoutId;
    }

    public CommonRecycleAdapter(Context context, List<T> dataList, int layoutId,int headerLayoutId,int footerLayoutId) {
        layoutInflater=LayoutInflater.from(context);
        this.dataList = dataList;
        this.layoutId = layoutId;
        this.headerLayoutId=headerLayoutId;
        this.footerLayoutId=footerLayoutId;
        ITEM_HEADER=headerLayoutId;
        ITEM_FOOTER=footerLayoutId;
    }

    @NonNull
    @Override
    public CommonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==ITEM_HEADER){
//            layoutId=headerLayoutId;
            View item=layoutInflater.inflate(headerLayoutId,parent,false);
            return new HeaderHolder(item);
        }else if(viewType==ITEM_FOOTER){
//            layoutId=footerLayoutId;
            View item=layoutInflater.inflate(footerLayoutId,parent,false);
            return new FooterHolder(item);
        }
//        else if(multiTypeSupport!=null){
//            layoutId=viewType;
//            View item=layoutInflater.inflate(layoutId,parent,false);
//            return new CommonViewHolder(item);
//        }
        else {
            View item = layoutInflater.inflate(layoutId, parent, false);
            return new CommonViewHolder(item);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder holder, int position) {
//        if(headerLayoutId!=0&&position==0){
//            bindHeaderData((HeaderHolder) holder);
//        }else if(footerLayoutId!=0&&position==getItemCount()-1){
//            bindFooterData((FooterHolder) holder);
//        }else {
//            position=headerLayoutId==0?position:position-1;
//            bindData(holder, dataList.get(position));
//        }

        //子类 instanceof 父类 == true
        //父类 instanceof 子类 == false
        if(holder instanceof HeaderHolder){
            bindHeaderData((HeaderHolder) holder);
        }else if(holder instanceof FooterHolder){
            bindFooterData((FooterHolder) holder);
        }else {
            position=headerLayoutId==0?position:position-1;
//            bindData(holder, dataList.get(position));
            bindData(holder, position);
        }
    }

    @Override
    public int getItemCount() {
        int size;
        size=dataList.size();
        if(headerLayoutId!=0){
            size++;
        }
        if(footerLayoutId!=0){
            size++;
        }
        return size;
    }

    @Override
    public int getItemViewType(int position) {
        if(headerLayoutId!=0&&position==0){
            return ITEM_HEADER;
        }else if(footerLayoutId!=0&&position==getItemCount()-1){
            return ITEM_FOOTER;
        }
//        else if(multiTypeSupport!=null){
//            position=headerLayoutId==0?position:position-1;
//            return multiTypeSupport.getLayoutId(dataList.get(position),position);
//        }
        else {
//            return layoutId;
            return ITEM_COMMON;
        }
//        return super.getItemViewType(position);
    }

    protected abstract void bindData(CommonViewHolder holder, T data);
    protected void bindData(CommonViewHolder holder, int position){
        bindData(holder, dataList.get(position));
    }

    protected abstract void bindFooterData(FooterHolder holder);

    protected abstract void bindHeaderData(HeaderHolder holder);
}
